package cn.shengchao.examstar.product.acl.adapters.repositories;

import cn.shengchao.examstar.product.acl.ports.repositories.INodeKnowledgePointRepository;
import cn.shengchao.examstar.product.infrastructure.dao.NodeKnowledgePointDao;
import cn.shengchao.examstar.product.infrastructure.dao.po.NodeKnowledgePoint;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class NodeKnowledgePointRepository implements INodeKnowledgePointRepository {

    @Autowired
    private NodeKnowledgePointDao nodeKnowledgePointDao;

    @Override
    public void edit(Long courseNodeId, List<Long> knowledgePointIds) {
        nodeKnowledgePointDao.delete(new LambdaQueryWrapper<NodeKnowledgePoint>()
            .eq(NodeKnowledgePoint::getCourseNodeId, courseNodeId));
        if (knowledgePointIds != null) {
            for (Long knowledgePointId : knowledgePointIds) {
                NodeKnowledgePoint nodeKnowledgePoint = new NodeKnowledgePoint();
                nodeKnowledgePoint.setCourseNodeId(courseNodeId);
                nodeKnowledgePoint.setKnowledgePointId(knowledgePointId);
                nodeKnowledgePointDao.insert(nodeKnowledgePoint);
            }
        }
    }

    @Override
    public List<NodeKnowledgePoint> getNodeKnowledgePoint(Long nodeId) {
        return nodeKnowledgePointDao.selectList(new LambdaQueryWrapper<NodeKnowledgePoint>()
            .eq(NodeKnowledgePoint::getCourseNodeId, nodeId));
    }
    
    @Override
    public List<NodeKnowledgePoint> getNodeKnowledgePointByKnowledgePointId(Long knowledgePointId) {
        return nodeKnowledgePointDao.selectList(new LambdaQueryWrapper<NodeKnowledgePoint>()
            .eq(NodeKnowledgePoint::getKnowledgePointId, knowledgePointId));
    }
}
